//
//  DestPhotoesViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/23.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestPhotoesViewController.h"
#import "SDPhotoItem.h"
#import "SDPhotoGroup.h"
#import "DestAFNDataBlock.h"
#import "BaseHeader.h"
@interface DestPhotoesViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic)NSMutableArray *destPhotoesStringArray;///三方 给图片数组的值;
@property(strong,nonatomic)UITableView *photoTaelView;
@property(strong,nonatomic)NSNumber *start;
@end

@implementation DestPhotoesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.title = [NSString stringWithFormat:@"%@图片",_cityName];
    self.view.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =CGRectMake(0, 20, 25, 25);
    [button setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(DestGoBack) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    _destPhotoesStringArray = [[NSMutableArray alloc]init];
    
    _start = @0;
    _photoTaelView = [[UITableView alloc]initWithFrame:self.view.frame];
    _photoTaelView.delegate = self;
    _photoTaelView.dataSource = self;
    _photoTaelView.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    _photoTaelView.showsHorizontalScrollIndicator  = NO;
    _photoTaelView.showsVerticalScrollIndicator = NO;
    [_photoTaelView setSeparatorStyle:0];
    [self.view addSubview:_photoTaelView];
    [self addFooter];
    
    
}
- (void)addFooter {
    __block DestPhotoesViewController *destPhotoes = self;
    [_photoTaelView addLegendFooterWithRefreshingBlock:^{
        
        [destPhotoes getData:destPhotoes.start];
    }];
    [_photoTaelView.footer beginRefreshing];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_destPhotoesStringArray.count % 3 == 0) {
        return 110 *_destPhotoesStringArray.count / 3;
    }else{
        return 110*(_destPhotoesStringArray.count / 3 +1);
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    
    SDPhotoGroup *photoGroup = [[SDPhotoGroup alloc]initWithFrame:CGRectMake(0, 64 * HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 64 * HEIGHT)];
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    [_destPhotoesStringArray enumerateObjectsUsingBlock:^(id  obj, NSUInteger idx, BOOL * stop) {
        SDPhotoItem *item = [[SDPhotoItem alloc]init];
        item.thumbnail_pic = obj;
        [temp addObject:item];
        
    }];
    photoGroup.photoItemArray = [temp copy];
    cell.backgroundColor =  [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    [cell.contentView addSubview:photoGroup];
    
    return cell;
}

- (void)getData: (NSNumber*)start {
    ///皮卡丘
        
    [DestAFNDataBlock string:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/photos/?start=%@&count=21&gallery_mode=true",_myType,_MyId,start] AFNData:^(id block) {
        
        NSMutableArray *itemsArray = [block objectForKey:@"items"];

        for (NSMutableDictionary *itemsDic in itemsArray) {
            [_destPhotoesStringArray addObject:[itemsDic objectForKey:@"photo_w640"]];
        }
        if ([[block objectForKey:@"next_start"] isKindOfClass:[NSNull class]]) {
            [_photoTaelView.footer endRefreshing];
            [_photoTaelView removeFooter];//移除加载
            return ;
            
        }else{
            
            _start = [block objectForKey:@"next_start"];
        }
        
        
        //  [self createphotoView];
        [_photoTaelView.footer endRefreshing];
        [_photoTaelView reloadData];
    } AFNData1:^(id block) {
       
        
    }];
}
#pragma mark - 返回方法
- (void)DestGoBack {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
